import { getToken } from '@/utils/cookie'
import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/oj/login",
      name: "Login",
      component: () => import("@/views/Login.vue"), 
    },
    {
      path: "/",
      redirect: "/oj/login",
    },
    {
      path: "/oj/layout",
      name: "Layout",
      component: () => import("@/views/Layout.vue"), 
      children: [
        {
          path: "question",
          name: "Question",
          component: () => import("@/views/Question.vue"), 
        },
        {
          path: "exam",
          name: "Exam",
          component: () => import("@/views/Exam.vue"), 
        },
        {
          path: "updateExam",
          name: "UpdateExam",
          component: () => import("@/views/UpdateExam.vue"), 
        },
        {
          path: "cuser",
          name: "Cuser",
          component: () => import("@/views/Cuser.vue"), 
        },
      ],
    },
    
  ],
})
/**
 * 路由守卫 错误过期token在相应拦截处理
 */
router.beforeEach((to, from, next) => {
  if(getToken()){
    if(to.path === '/oj/login'){
      next({path: '/oj/layout/cuser'})
    }else{
      next()
    }     
  } else{
    if(to.path !== '/oj/login'){
      next({path: '/oj/login'})
    }else{
      next()
    }
  } 
})

export default router
